package com.sxyjhh.schedule;

import com.sxyjhh.common.DBConfig;
import com.sxyjhh.service.data_exchange.DataExchangeService;
import com.sxyjhh.service.data_exchange.ImageService;
import com.sxyjhh.utils.CommonUtils;
import com.sxyjhh.utils.redis.RedisOperator;
import com.sxyjhh.utils.util.XaUtil;
import net.sf.json.JSONObject;
import org.apache.commons.collections.map.HashedMap;
import org.apache.log4j.Logger;
import org.apache.tools.ant.taskdefs.Sleep;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;

import java.sql.SQLException;
import java.util.*;

/**
 * Created by Administrator on 2018/6/29.
 */

@Component
public class ScheduleTask {

    String COMPANY_SAFETY_CERTIFICATE_OUT = DBConfig.readPop("COMPANY.SAFETY.CERTIFICATE.OUT");

    String PERSON_SECURITY_CERTIFICATE_OUT = DBConfig.readPop("PERSON.SECURITY.CERTIFICATE.OUT");

    String PERSON_SPECIAL_CERTIFICATE_OUT = DBConfig.readPop("PERSON.SPECIAL.CERTIFICATE.OUT");

    String IMAGES_TABLE_NAME_OUT = DBConfig.readPop("IMAGES.TABLE.NAME.OUT");

    private static Logger log = Logger.getLogger(ScheduleTask.class);

   /* @Autowired
    public Jedis jedis;*/

    @Autowired
    public DataExchangeService dataExchangeService;

    @Autowired
    public ImageService imageService;

    @Autowired
    private JedisCluster jedisCluster;

    @Autowired
    private RedisOperator redisOperator;


    //企业安全生产许可信息

    @Scheduled(cron = "${safety.every.day}")
    public void safetyCertiTask() throws SQLException {

        int i = delRedisData(COMPANY_SAFETY_CERTIFICATE_OUT);
        log.info("===========删除缓存表===========");
        log.info(COMPANY_SAFETY_CERTIFICATE_OUT);
        log.info("缓存数据： "+i);
        dataExchangeService.startSynData();
    }

    //建筑施工企业安管人员安全生产考核信息
    @Scheduled(cron = "${person.every.day}")
    public void chartProScheduleTask() throws SQLException {
        int i = delRedisData(PERSON_SECURITY_CERTIFICATE_OUT);
        log.info("===========删除缓存表===========");
        log.info(PERSON_SECURITY_CERTIFICATE_OUT);
        log.info("缓存数据： "+i);
        dataExchangeService.startSynDataRy();
    }

    //图片信息
    @Scheduled(cron = "${image.every.day}")
    public void chartScheduleTask() throws SQLException, InterruptedException {
        int i = delRedisData(IMAGES_TABLE_NAME_OUT);
        log.info("===========删除缓存表===========");
        log.info(IMAGES_TABLE_NAME_OUT);
        log.info("缓存数据： "+i);
        imageService.startSynDataTP();
        System.out.println("===========");
    }


    //特种作业人员信息
    @Scheduled(cron = "${person.tz.every.day}")
    public void chartTzScheduleTask() throws SQLException {
        int i = delRedisData(PERSON_SPECIAL_CERTIFICATE_OUT);
        log.info("===========删除缓存表===========");
        log.info(PERSON_SPECIAL_CERTIFICATE_OUT);
        log.info("缓存数据： "+i);
        dataExchangeService.startSynDataTz();
    }

    //删除信息
    public int delRedisData(String tabName) {
        //删除记录
        Set<String> set = redisOperator.keys(tabName + "*");
        int i = 0;
        if (!XaUtil.isEmpty(set)) {
            for (String redisKey : set) {
                i += jedisCluster.del(redisKey);
            }
        }
        return i;
    }
}
